
package ro.ase.catalog.jfreechart.style;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.RectangleEdge;

import java.awt.*;

public class PieStyle {

    public static void styleDefault(JFreeChart chart){
        PieStyle.setBackground(chart);
                
    }

    public static void styleOne(JFreeChart chart){
        PiePlot plot = (PiePlot)chart.getPlot();
        PieDataset pd = plot.getDataset();
        PieStyle.setBackground(chart);
        if(pd!=null){

            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));

            for (int i = 0; i < pd.getItemCount(); i++) {
                int color = i % colors.length;
                plot.setSectionPaint(pd.getKey(i), colors[color]);
            }
        }
        else{
            plot.setNoDataMessage("NO DATA");
        }
        
    }

    public static void styleTwo(JFreeChart chart){
        PiePlot plot = (PiePlot)chart.getPlot();
        
        PieStyle.setBackground(chart);
        

        BlockContainer block = new BlockContainer(new BorderArrangement());
        block.setBorder(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D));


        LegendTitle legend = new LegendTitle(plot);        
        BlockContainer legendBlock = legend.getItemContainer();

        block.add(legendBlock);
        legend.setWrapper(block);
        legend.setPosition(RectangleEdge.BOTTOM);

        chart.addSubtitle(legend);

        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
        
    }

    public static void setBackground(JFreeChart chart){
        chart.setBackgroundPaint(null);
    }     

    public static Paint colors[] = { 
        Color.decode("#88AACC"), Color.decode("#999933"),
        Color.decode("#666699"), Color.decode("#CC9933"),
        Color.decode("#006666"), Color.decode("#3399FF"),
        Color.decode("#993300"), Color.decode("#AAAA77"),
        Color.decode("#666666"), Color.decode("#FFCC66"),
        Color.decode("#6699CC"), Color.decode("#663366"),
        Color.decode("#9999CC"), Color.decode("#AAAAAA"),
        Color.decode("#669999"), Color.decode("#BBBB55"),
        Color.decode("#CC6600"), Color.decode("#9999FF"),
        Color.decode("#0066CC"), Color.decode("#99CCCC"),
        Color.decode("#999999"), Color.decode("#FFCC00"),
        Color.decode("#009999"), Color.decode("#99CC33"),
        Color.decode("#FF9900"), Color.decode("#999966"),
        Color.decode("#66CCCC"), Color.decode("#339966"),
        Color.decode("#CCCC33") 
    };
}
